//index.js
//获取应用实例
var postData = require('../../data/post-data.js')
//var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var call = require("../../utils/request.js")
//var qqmapsdk;
var app = getApp()
Page({
  data: {
    afferent: '',
    clicked: 0,
    clickede: 1,
    clickedes: 1,
    clickedese: 2,
    chooseradio: '0',
    mobile: '',
    area: '',
    radvalue: 1,
    radname: '',
    main_room: "1室",
    main_tin: "2厅",
    main_wei: "2卫",
    main_yang: "3阳台",
    houstyle: '1223',
    room: 1,
    tin: 1,
    wei: 1,
    yan: 1,
    city: '',
    cityid: '',
    proviceid: '',
    sid: '',
    // imgUrls: [
    //   'https://img.xtuan.com/xcximgtest/images/ad.png',
    //   'https://img.xtuan.com/xcximgtest/images/banner_01.jpg',
    //   'https://img.xtuan.com/xcximgtest/images/banner_02.jpg',
    // ],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    currentSwiper: 0,
    landing_page: '',
    citys: '厦门',
    colist: [{
        name: '人工费',
        price: '?'
      },
      {
        name: '材料费',
        price: '?'
      },
      {
        name: '设计费',
        price: '?'
      },
      {
        name: '质检费',
        price: '?'
      }
    ],
  },
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: app.apiUrl('c=index&a=getconfig'),
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        var city = res.data.data.now_site.city
        var id = res.data.data.now_site.id
        var imgUrls = res.data.data.banner
        that.setData({
          citys: city,
          imgUrls: imgUrls
        })
        wx.setStorageSync('cityid', id);
      }
    })
    this.setData({
      showModal: false,
      Modalcontent: false,
      postlist: postData.postlist
      // city:citys
    })
   
    call.getData('session', this.shuffleSuc, this.fail);
    call.getOrigin('origin', this.origisucess, this.originfail);
    //console.log(postData.postlist)
  },
  //获取session
  shuffleSuc: function (res) {
    //console.log('session请求',res)
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      sid: sid
    })

  },
  fail: function () {
    console.log("失败")
  },
  //获取页面来源
  origisucess: function (res) {
    //console.log(res)
    var that = this;
    console.log('来源页请求成功', res)
    var land_page = res.datas.landing_page
    var housetype =res.datas.house_type
    var decorate = res.datas.decorate_type
    that.setData({
      landing_page: land_page,
      housetype:housetype,
      decore:decorate
    })

  },
  originfail: function () {
    console.log("失败")
  },
  //头部城市选择
  choosethecity: function() {
    var id = 1
    var citys = this.data.afferent
    wx.navigateTo({
      url: 'switchcity?data=' + [id, citys]
    })
  },
  //表单城市选择
  choosecity: function() {
    wx.navigateTo({
      url: 'public/custom/cucity'
    })
  },
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  //显示蒙版
  chooroom: function() {
    var that = this;
    this.setData({
      showModal: true,
      Modalcontent: true
    })
  },
  //弹框隐藏
  confirm: function() {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false
    })
  },
  cancle: function() {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false
    })
  },
  //链接跳转
  epecial: function() {
    // wx.showToast({
    //   title: '尽情期待！',
    //   icon: 'success',
    //   duration: 2000
    // })
    wx.navigateTo({
      url: '../logs/logs' //到材料编辑页
    })
    // wx.switchTab({
    //   url: '../impredra/impredra' //到材料编辑页
    // })
  },
  skipTodecom: function(e) {
    wx.switchTab({
      url: 'decorcompany/decorcompany' //到装修公司
    })
  },
  skipTostre: function(e) {
    wx.switchTab({
      url: '../strategy/strategy' //到装修攻略
    })
  },
  skipquote: function(e) {
   wx.navigateTo({
      url: 'free/quote', //到免费报价
    })
  },
  skipvoroom: function(e) {
    wx: wx.navigateTo({
      url: 'free/voluroom', //到免费量房
    })
  },
  skipfreede: function(e) {
    wx: wx.navigateTo({
      url: 'free/free_design', //到免费设计
    })
  },
  skipdecolan: function(e) {
    wx.navigateTo({
      url: 'free/decoloan', //到装修贷款
    })
  },
  skipinsurance: function(e) {
    wx.navigateTo({
      url: 'free/insurance', //到装修保
    })
  },
  skipsupervision: function() {
    wx.navigateTo({
      url: 'free/supervision', //到免费监理
    })
  },
  //弹框内容点击选择
  choose_btn: function(e) {
    var that = this;
    var room = e.target.dataset.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    //console.log(room)
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].room;
    if (this.data.clicked == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clicked: e.currentTarget.id,
      main_room: value,
      room: room,
      houstyle: housty
    })
    console.log(e)
  },
  choose_tin: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].hall;
    var room = that.data.room
    var tin = e.target.dataset.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickede == e.currentTarget.id) {
      this.setData({
        clickIde: +1,
      })
      return;
    }
    that.setData({
      clickede: e.currentTarget.id,
      main_tin: value,
      tin: tin,
      houstyle: housty
    })
    console.log(value)
  },
  choose_wei: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].toilet;
    var room = that.data.room
    var tin = that.data.tin
    var wei = e.target.dataset.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        clickIdes: +1,
      })
      return;
    }
    that.setData({
      clickedes: e.currentTarget.id,
      main_wei: value,
      wei: wei,
      houstyle: housty
    })
    console.log(value)
  },

  choose_sun: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].balcony;
    var room = that.data.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = e.target.dataset.tty
    var housty = +room + '' + tin + '' + wei + '' + yan + ''

    if (this.data.clickedese == e.currentTarget.id) {
      this.setData({
        clickIdese: +1,
      })
      return;
    }
    that.setData({
      clickedese: e.currentTarget.id,
      main_yang: value,
      yan: yan,
      houstyle: housty
    })
    console.log(value)
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  //阻止默认事件
  stopmove: function(e) {

  },
  preventTouchMove: function(e) {

  },
  onShow: function() {
    // 生命周期函数--监听页面显示
    var that = this;
    var value = wx.getStorageSync('concent');
    //console.log('这个value', value)
    this.setData({
      citys: value,
      afferent: value
    })
  },


  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //整屋详情
  binddetail: function(e) {
    var arcleId = e.currentTarget.dataset.arcleid;
    wx.navigateTo({
      url: "../detail/index_detail?id=" + arcleId
    })
    console.log(arcleId);
  },
  //弹框隐藏
  bindmask: function () {
    this.setData({
      showinfo: false,
      showModal: false,
      Modalcontent: false
    })
  },
  //面积，房屋装修方式，电话号码取值
  radioChange: function(e) {
    var radvalue = this.data.radvalue
    var values = e.currentTarget.dataset.id
    var radname = e.currentTarget.dataset.name
    console.log(values)
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        chooseradio: +1,
      })
      return;
    }
    this.setData({
      radvalue: values,
      chooseradio: e.currentTarget.id
    })
  },
  givearea: function(e) {
    var value = e.detail.value
    this.setData({
      area: value
    })
  },
  givetele: function(e) {
    var value = e.detail.value
    this.setData({
      mobile: value
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //form表单提交,到报价结果页
  skipsrsult: function(e) {
    var that = this
    var arr = []
    var city = that.data.afferent
    var alea = that.data.area
    var mobile = that.data.mobile
    var radios = that.data.radvalue
    console.log(radios)
    var mainroom = that.data.main_room
    var ting = that.data.main_tin
    var wei = that.data.main_wei
    var yang = that.data.main_yang
    var houstyle = that.data.houstyle
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var sid = that.data.sid
    var landing_page = that.data.landing_page
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log(mobile)
    if (city == '') {
      that.setData({
        alertmess: true,
        message: '请选择城市！'
      })
      that.settime()
    } else if (alea == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (radios == '') {
      that.setData({
        alertmess: true,
        message: '装修类型未选择！'
      })
      that.settime()
    } else if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '请填写手机号码！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号码有误,请重新输入！'
      })
      that.settime()
    } else {
      //that.getevaluate()
      wx.request({
        url: app.apiUrl("c=zhaobiao/index&a=submit_estimate"),
        //url: 'http://www.newx.com/api/weixinapp/?c=zhaobiao/index&a=submit_estimate',
        data: {
          phone: mobile,
          acreage: alea,
          house_type: houstyle,
          decorate_type: radios,
          province: proviceid,
          city: cityid,
          landing_page: landing_page
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: 'POST',
        success: function(res) {
          console.log(res.data)
          var code = res.data.code
          var error = res.data.error
          if (code == 200) {
            var bid_id = res.data.datas.bid_id
            var unid = res.data.datas.uuid
            var id = 5
            wx.navigateTo({
              url: 'free/quote_result?data=' + [id, alea, radios, mainroom, ting, wei, yang, city, mobile, bid_id, unid]
            })
          } else {
            that.setData({
              alertmess: true,
              message: error
            })
            that.settime()
          }
        }
      })
    }
    console.log(arr)
  },
})